#include "stm32f10x.h"                  // Device header
#include "LED.h"
#include "Delay.h"
#include <stdint.h>
#include "Key.h"
#include "OLED.h"
#include "Serial.h"
#include "chuan.h"
#include "duo.h"
#include <stdio.h>
#include "PCA9685.h"
#include "ADC.h"
#include "bluetooth.h"
uint8_t RxData;
int value;
float weight;
int32_t reset;
float Weights=100.0;  //100g
int32_t Weights_100=8493860;  //100g
uint16_t time=0;
uint8_t eat=0;
void Movedown(void)
{
		PCA9685_setPWM(1,0,100);//right
		PCA9685_setPWM(4,0,500);//;left
		Delay_ms(1000);
		PCA9685_setPWM(1,0,300);
		PCA9685_setPWM(4,0,300);
		Delay_ms(1000);
}

void Moveup(void)
	
{
		PCA9685_setPWM(1,0,500);
		PCA9685_setPWM(4,0,150);
		Delay_ms(500);
		PCA9685_setPWM(1,0,300);
		PCA9685_setPWM(4,0,300);
		Delay_ms(1000);
		time++;
}

void Movedou(void)
{
		PCA9685_setPWM(2,0,350);//left
		PCA9685_setPWM(3,0,250);//right
		Delay_ms(1000);
		PCA9685_setPWM(2,0,250);
		PCA9685_setPWM(3,0,350);
		Delay_ms(1000);
}
void dao(void)
{
	PCA9685_setPWM(5,0,500);//left
	Delay_ms(1000);
}
void food(void)
{
		PCA9685_setPWM(8,0,480);//food500
		Delay_ms(1000);
		PCA9685_setPWM(8,0,500);
}
void food1(void)
{
			PCA9685_setPWM(7,0,500);//food500
			Delay_ms(1000);
			PCA9685_setPWM(7,0,200);

}
int main(void)
{
	 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  OLED_Init();
	LED_Init();
	Serial_Init();
	Serial2_Init();
	uart3_init();
	pwm_init();
	AD_Init();
	FAN_Init();
	Food_Init();
 char *temp;
	int angle=300,i=0,k=200,a=0,du,cha=0;
	PCA9685_Init(50,180);
	uint8_t left=1,right=1,Ri=0;
	uint16_t biaoleft=0;
	unsigned char  buf[64];
	OLED_ShowNum(1,1,1,1);		
	PCA9685_setPWM(8,0,500);

	LED_OFF();
//	Serial_SendString("hello_word\r\n");     
	while(1)
	{
		eat=Food();
		Delay_ms(1000);		
		du=AD_GetValue();
//		if(du<500)
//		{
//			Fan_ON();
//		}
//		if(du>500)
//		{
//			Fan_OFF();
//		}
		if(flag==1)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			Moveup();
			Delay_ms(1000);
			flag=0;
		}
		if(flag==2)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			Movedown();
			Delay_ms(1000);
			flag=0;
		}
		if(flag==3)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			cha+=20;
			PCA9685_setPWM(2,0,angle+cha);//left
			PCA9685_setPWM(3,0,angle-cha);//right
			Delay_ms(100);
			flag=0;
		}
		if(flag==4)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			cha-=20;
			PCA9685_setPWM(2,0,angle+cha);//left
			PCA9685_setPWM(3,0,angle-cha);//right
			Delay_ms(100);
			flag=0;
		}
		if(flag==5)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			dao();
			flag=0;
		}
		if(flag==6)
		{
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			Movedown();
			Movedown();
//			PCA9685_setPWM(1,0,200);//right
//			PCA9685_setPWM(4,0,400);//;left
			Delay_ms(500);
			PCA9685_setPWM(1,0,300);
			PCA9685_setPWM(4,0,300);
			Delay_ms(1000);
			dao();
			PCA9685_setPWM(2,0,300-60);//left
			PCA9685_setPWM(3,0,300+60);//right
			Delay_ms(500);
			PCA9685_setPWM(2,0,300);//left
			PCA9685_setPWM(3,0,300);//right
			Delay_ms(500);
			PCA9685_setPWM(2,0,300-60);//left
			PCA9685_setPWM(3,0,300+60);//right
			Delay_ms(500);
			PCA9685_setPWM(2,0,300);//left
			PCA9685_setPWM(3,0,300);//right
			Delay_ms(500);
			PCA9685_setPWM(5,0,300);//left
			
			Delay_ms(500);
			PCA9685_setPWM(1,0,500);
			PCA9685_setPWM(4,0,150);
			Delay_ms(1000);
			PCA9685_setPWM(1,0,300);
			PCA9685_setPWM(4,0,300);
			Delay_ms(1000);
			PCA9685_setPWM(1,0,500);
			PCA9685_setPWM(4,0,150);
			Delay_ms(500);
			PCA9685_setPWM(1,0,300);
			PCA9685_setPWM(4,0,300);
			Delay_ms(1000);
			time++;
			sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
			HMISends((char *)buf); //??Ri????page0???t3????
			HMISendb(0xff);//
			flag=0;
		}
		if(flag==7)
		{
			food1();
			Delay_ms(500);
			flag=0;
		}
		LED_OFF();
		sprintf((char *)buf,"page0.b1.txt=\"down\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		sprintf((char *)buf,"page0.b0.txt=\"up\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		sprintf((char *)buf,"page0.b2.txt=\"qian\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		sprintf((char *)buf,"page0.b3.txt=\"hou\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		sprintf((char *)buf,"page0.b4.txt=\"dao\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		sprintf((char *)buf,"page0.b5.txt=\"zheng\""); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//???
		
		
		sprintf((char *)buf,"page0.t0.txt=\"weight=%lf\"",weight); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//
		
		sprintf((char *)buf,"page0.t2.txt=\"%d\"",flag); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//
		
		sprintf((char *)buf,"page0.t3.txt=\"%d\"",cha); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//
		
		sprintf((char *)buf,"page0.t4.txt=\"%d\"",du); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//
		
		sprintf((char *)buf,"page0.t5.txt=\"%d\"",eat); //??????,??????
		HMISends((char *)buf); //??Ri????page0???t3????
		HMISendb(0xff);//
		}
		
}
//}
